﻿#ifndef AVIDEOCANVAS_H
#define AVIDEOCANVAS_H

#include <QQuickPaintedItem>
#include <mutex>

extern "C"
{
#include <libavformat/avformat.h>
};

class VideoRenderOpenGL;

class BCQmlRenderView : public QQuickPaintedItem
{
    Q_OBJECT
public:
    BCQmlRenderView(QQuickItem *parent = nullptr);
    ~BCQmlRenderView();
    int setViewProperties(int zOrder, float left, float top, float right, float bottom);
    int deliverFrame(AVFrame * frame, int rotation, bool mirrored);
    virtual void paint(QPainter *painter) override;
protected:
    virtual void geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) override;
signals:
    void frameDelivered();
    void widgetInvalidated();
    void viewSizeChanged(int width, int height);

public slots:
    void renderFrame();
    void handleWindowChanged(QQuickWindow *win);
    void cleanup();
private:
    std::unique_ptr<VideoRenderOpenGL> m_render;
    std::mutex m_mutex;
    //usage of m_frame should be guarded by m_mutex
    AVFrame *m_frame;
    int m_rotation;
    bool m_mirrored;
};

#endif // AVIDEOCANVAS_H
